package com.chinasoft.cr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinasoft.cr.mapper.UserMapper;
import com.chinasoft.cr.pojo.PageItem;
import com.chinasoft.cr.pojo.User;
import com.chinasoft.cr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<User> getById(long id){
        List<User> userList = userMapper.selectList(new QueryWrapper<User>().eq("id",id));
        return  userList;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    @Override
    public List<User> getPage(Integer current, Integer size) {
        //  分页操作
        Page<User> page = new Page<>(current, size);
        // 执行分页
        //  分页的查询结果 放到输入参数 page
        userMapper.selectPage(page, null);
        //  数据记录
        List<User> userList = page.getRecords();
        long total = page.getTotal();
        System.out.println(total);
        System.out.println(userList);
        return userList;
    }

    @Override
    public PageItem<User> getPage2(Integer pageNum, Integer pageSize, User query) {
        //  分页操作
        Page<User> page = new Page<>(pageNum, pageSize);
        // 创建一个查询条件对象
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        // 对 id进行模糊查询
//        if (query.getId() != null) {
//            lqw.like(User::getId, query.getId());
//        }
        lqw.like(query.getId() != null, User::getId, query.getId());
        lqw.like(query.getUsername() != null && query.getUsername().trim().length() > 0
                , User::getUsername, query.getUsername());
        lqw.like(query.getNickName() != null, User::getNickName, query.getNickName());
        lqw.like(query.getPhone() != null, User::getPhone, query.getPhone());
        lqw.like(query.getAddress() != null, User::getAddress, query.getAddress());
        lqw.like(query.getRole() != null, User::getRole, query.getRole());
        // 执行分页
        //  分页的查询结果 放到输入参数 page
        userMapper.selectPage(page, lqw);
        //  数据记录
        List<User> userList = page.getRecords();
        long total = page.getTotal();
        System.out.println(total);
        System.out.println(userList);
        PageItem<User> pi = new PageItem<>(userList, total);
        return pi;
    }

    @Override
    public Boolean deleteUser(Long id) {
        // 进行用户查询
        User user = userMapper.selectById(id);
        if (user == null) {
            // 用户不存在
            return false;
        }
        int ret = userMapper.deleteById(id);
        if (ret == 1) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean updateUser(User user) {
        // 查询用户
        User u1 = userMapper.selectById(user.getId());

        if (u1 == null) {
            // 用户不存在
            return false;
        }
        // 修改用户的数据
        int ret = userMapper.updateById(user);
        if (ret == 1) {
            // 修改成功
            return true;
        }
        // 修改失败
        return false;
    }
//    @Override
//    public Boolean updateUser(User user) {
//        // 更新用户信息
//         User existingUser = userMapper.selectById(user.getId());
//        if (existingUser == null) {
//             return false; // 用户不存在
//         }
//         return userMapper.updateById(user) == 1;
//    }

    @Override
    public User checkUser(User user, String type) {
        // 根据用户名字查询记录
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        // 设置查询条件
        lqw.eq(User::getUsername, user.getUsername());
        // 执行查询
        User userDb = userMapper.selectOne(lqw);
        if(type.equals("login")) {
            if (userDb == null) {
                //  登录失败
                return null;
            }
            // 比较密码
            if (user.getPassword().equals(userDb.getPassword())) {
                // 登录成功
                return userDb;
            }
            // 密码不对
            return null;
        } else {
            return userDb;
        }
    }

    @Override
    public User getUser(Long userId) {
        return userMapper.selectById(userId);
    }


    @Override
    public int UserCount() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return Math.toIntExact(userMapper.selectCount(queryWrapper));
    }

    @Override
    public int save(User params) {
        return userMapper.insert(params);
    }


}
